package com.fmn.dots_final_i_hope;

import model.GameField;
import model.LineHorizontal;
import model.LineVertical;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	

	ImageView[][] gameField;
	
	public EditText pl1;
	public EditText pl2;
	public EditText currPlay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		PlayerControl.p1.setChosenImage(PlayerControl.chosenPic1);
		PlayerControl.p2.setChosenImage(PlayerControl.chosenPic2);


		
		if (PlayerControl.choosenCol==3&&PlayerControl.choosenRow==3){
			setContentView(R.layout.field_3x3);
		} else if (PlayerControl.choosenCol==4&&PlayerControl.choosenRow==3){
			setContentView(R.layout.field_4x3);

		}else if (PlayerControl.choosenCol==4&&PlayerControl.choosenRow==4){
			setContentView(R.layout.field_4x4);

		}else if (PlayerControl.choosenCol==5&&PlayerControl.choosenRow==4){
			setContentView(R.layout.field_5x4);

		}else if (PlayerControl.choosenCol==5&&PlayerControl.choosenRow==5){
			setContentView(R.layout.field_5x5);

		}

		
		gameField = new ImageView[PlayerControl.choosenCol * 2 + 1][PlayerControl.choosenRow * 2 + 1];
		
		PlayerControl.maxPointsPossible = (PlayerControl.choosenCol)*(PlayerControl.choosenRow);






		
		  


		  for(int i=0; i<gameField.length; i++) {
			   for(int j=0; j<gameField[i].length; j++) {
			    String drawObjectID = "drawObject_" + i + "_" + j;
			    int resID = getResources().getIdentifier(drawObjectID, "id", getPackageName());
			    gameField[i][j] = ((ImageView) findViewById(resID));
			   
			   }
			}
		
		  

		
		//---these are just for test visualisation
		
		pl1 = (EditText) findViewById(R.id.editTextP1);
		pl2 = (EditText) findViewById(R.id.editTextP2);
		
		currPlay = (EditText) findViewById(R.id.editTexCurr);
		
		pl2.setText(Integer.toString(PlayerControl.p2.getCurrentPoints()));
		pl1.setText(Integer.toString(PlayerControl.p1.getCurrentPoints()));
		
		currPlay.setText(PlayerControl.getCurrentPlayer().getName());
		
		//----
		
		
		

		// Setting connections between Lines and Squares

		GameField.setReferences(gameField);

		// Setting touch listeners for Lines - could be re-factored into method

		for (int i = 0; i < gameField.length; i++) {
			for (int j = 0; j < gameField[i].length; j++) {

				if (i % 2 == 0 && j % 2 != 0) {
					gameField[i][j].setOnTouchListener(horizontalPressed);
				}
				if (i % 2 != 0 && j % 2 == 0) {
					gameField[i][j].setOnTouchListener(verticalPressed);
				}

			}
		}
		
		

		
		
		

	}


	
	View.OnTouchListener horizontalPressed = new View.OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			LineHorizontal lineH = (LineHorizontal) v;

			if (event.getAction() == MotionEvent.ACTION_DOWN) {

				if (lineH.isPressed() == false) {
					lineH.setPressed(true);
					
					if(lineH.tryToObtainSquare()== false)
					PlayerControl.changePlayer();
					
					
	
					currPlay.setText(PlayerControl.getCurrentPlayer().getName());
					
					pl2.setText(Integer.toString(PlayerControl.p2.getCurrentPoints()));
					pl1.setText(Integer.toString(PlayerControl.p1.getCurrentPoints()));
					
				}
				
				if(PlayerControl.checkForEndGame())
					showEndGameDialog();
					
				return true;
			}

			return false;
		}

	};

	View.OnTouchListener verticalPressed = new View.OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			LineVertical lineV = (LineVertical) v;

			if (event.getAction() == MotionEvent.ACTION_DOWN) {

				if (lineV.isPressed() == false) {
					lineV.setPressed(true);
					
					if(lineV.tryToObtainSquare() == false)
					PlayerControl.changePlayer();
					
					
					currPlay.setText(PlayerControl.getCurrentPlayer().getName());
					
					pl2.setText(Integer.toString(PlayerControl.p2.getCurrentPoints()));
					pl1.setText(Integer.toString(PlayerControl.p1.getCurrentPoints()));
					
				}
				
				if(PlayerControl.checkForEndGame())
					showEndGameDialog();
					
				return true;
			}

			return false;
		}

	};
	
	private void showEndGameDialog(){
		AlertDialog.Builder dBuilder = new AlertDialog.Builder(this);
		
		String msg = "The winner is: " + PlayerControl.anounceWinner() + "\nPoints: " + PlayerControl.p1.getCurrentPoints() + " : " + PlayerControl.p2.getCurrentPoints();
	
		dBuilder.setTitle("GAME OVER");
		dBuilder.setMessage(msg);

		
		dBuilder.setPositiveButton("REMATCH", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				PlayerControl.nullPoints();
				Intent rematchIntent = new Intent(MainActivity.this, MainActivity.class);
				MainActivity.this.startActivity(rematchIntent);
				
			}
		});
		
		dBuilder.setNegativeButton("TO MAIN MENU", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				PlayerControl.nullPoints();
				Intent goToMainMenuIntent = new Intent(MainActivity.this, MenuActivity.class);
				MainActivity.this.startActivity(goToMainMenuIntent);
				
			}
		});
	
		AlertDialog endGame = dBuilder.create();
		endGame.show();
		
	}
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
